#  -*- coding: utf-8 -*-
# @Time    : 2020/11/19 20:36
# @Author  : 韋筠曦
# @Site    : 
# @File    : red_csv.py
# @Software: PyCharm
import csv


class Snippet:

    def __init__(self):
        self.f = open('filename.csv', 'r', encoding='utf-8')  # 详情请看参考知识206行

        self.headers = ['class', 'name', 'sex', 'height', 'year'] # csv 表头
        self.dictreader = csv.DictReader(self.f, self.headers)  # 迭代器

    def readcsv(self):
        '''
        从 csv 文件读取数据到程序
        :return: None
        '''
        row_header = next(self.dictreader)  # 读取表头第一行
        print(row_header)
        for line in self.dictreader:        # 遍历迭代器
            print(line)                     # 获得每一行值

    def __del__(self):
        self.f.close()                      # 关闭流


if __name__ == '__main__':
    snippet = Snippet()
    snippet.readcsv()